<?php
$sup_id = intval( dPgetParam( $_GET, 'sup_id', 0 ) );

// check permissions for this record
/*
$perms =& $AppUI->acl();
if (! ($canEdit = $perms->checkModuleItem( 'currencys', 'edit', $currency_id )) ) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect();
}
*/

// load the record data
$msg = '';
$row = new CSupplier();

$canDelete = $row->canDelete( $msg, $currency_id );
if($msg == $AppUI->_('SupplierDeleteUserError', UI_OUTPUT_JS)) {
	$userDeleteProtect=true;
}

if ($view) {
	$view = ' readonly="true"';
}
else {
	unset($view);
}

if (!$row->load( $sup_id ) && $sup_id > 0) {
	$AppUI->setMsg( 'Supplier' );
	$AppUI->setMsg( 'invalidID', UI_MSG_ERROR, true );
	$AppUI->redirect();
}
$status_list=null;
$type_list=null;
$cls_list = null;
$division_list = null;
$costcenter_list = null;
if (!$view) {
	require_once(DP_BASE_DIR."/modules/masterdata/mdstatus.class.php");
	$status_list = getStatusList($typeSupplier);

	require_once(DP_BASE_DIR."/modules/masterdata/mdtype.class.php");
	$type_list = getTypeList($typeSupplier);

	require_once(DP_BASE_DIR."/modules/masterdata/classification/classification.class.php");
	$cls_list = arrayMerge(array( '' => '' ),getClassificationList($typeSupplier));

	require_once(DP_BASE_DIR."/modules/masterdata/mddivision.class.php");
	$division_list = arrayMerge(array( '' => '' ), getDivisionList());

	$q = new DBQuery();
	$q->addTable('users', 'u');
	$q->addTable('contacts', 'c');
	//$q->addTable('project_contacts', 'pc');
	$q->addQuery('user_id, concat(contact_first_name, \' \', contact_last_name) as contact_name');
	//$q->addWhere('pc.contact_id = u.user_id AND u.user_contact = c.contact_id AND pc.project_id = '.$task_project);
	$q->addWhere('u.user_contact = c.contact_id');
	$q->addOrder('contact_first_name');
	$sql = $q->prepare();
	$resourceList = db_loadHashList($sql);
	$resourceList = arrayMerge(array(''=>''), $resourceList);

	$q = new DBQuery();
	$q->addTable('suppliers');
	$q->addQuery('sup_code');
	if (isset($row->sup_id)) {
		$q->addWhere("sup_id != $row->sup_id");
	}
	$quoteCodeList = $q->loadColumn();
	$qCL = '["' . implode('", "', $quoteCodeList) . '"]';
?>
<form name="MdSupplierForm" action="?m=suppliers" method="post">
<input type="hidden" name="dosql" value="do_aed" />
<input type="hidden" name="sup_id" value="<?=$sup_id?>" />
<?}?>
<table border="0" cellpadding="8" cellspacing="0" width="400px">
<tr>
<td colspan="2" bgcolor="#E0E0E0"><b><?=$AppUI->_('Supplier Information')?></td>
</tr>
<tr>
	<td colspan="2">
		<table border="0" cellpadding="0" cellspacing="4" width="100%">
		<tr>
			<td align="right">* <?=$AppUI->_('Code')?></td>
			<td>
				<input type="text" class="text" name="sup_code" id="sup_code"
				value="<?=@$row->sup_code?>" maxlength="255" style="width: 300px" <?=$view?>/>
			</td>
		</tr>
		<tr>
			<td align="right">* <?=$AppUI->_('Name')?></td>
			<td>
				<input type="text" class="text" name="sup_name" id="sup_name"
					value="<?=@$row->sup_name?>" maxlength="255" style="width: 300px" <?=$view?>/>
			</td>
		</tr>
		<tr>
			<td align="right">* <?=$AppUI->_('Status')?></td>
			<td>

				<? echo arraySelect($status_list,'sup_status','id="sup_status	" class="text" style="text-transform: capitalize; width: 300px" title="Status :: Status is required"',@$row->sup_status)?>
			</td>
		</tr>
		<tr>
			<td align="right">* <?=$AppUI->_('Type')?></td>
			<td>
				<? echo arraySelect($type_list,'sup_type','id="sup_type" class="text" style="text-transform: capitalize; width: 300px" title="Type :: Type is required"',@$row->sup_type)?>
			</td>
		</tr>
		<tr>
			<td align="right"><?=$AppUI->_('Exact Code')?></td>
			<td>
				<input type="text" class="text" name="sup_exact_code"
					value="<?=@$row->sup_exact_code?>" maxlength="255" style="width: 150px" <?=$view?>/>
			</td>
		</tr>
		<tr>
			<td align="right"><?=$AppUI->_('VAT Code')?></td>
			<td>
				<input type="text" class="text" name="sup_vat_code"
					value="<?=@$row->sup_vat_code?>" maxlength="255" style="width: 150px" <?=$view?>/>
			</td>
		</tr>
		<tr>
			<td align="right"><?=$AppUI->_('Address')?></td>
			<td colspan="3">
				<input type="text" class="text" name="sup_address"
					value="<?=@$row->sup_address?>" maxlength="255" style="width: 300px" <?=$view?>/>
			</td>
		</tr>
		<tr>
			<td align="right"><?=$AppUI->_('Tel')?></td>
			<td>
				<input type="text" class="text" name="sup_tel"
					value="<?=@$row->sup_tel?>" maxlength="150" style="width: 150px" <?=$view?>/>
			</td>
		</tr>
		<tr>
			<td align="right"><?=$AppUI->_('Fax')?></td>
			<td>
				<input type="text" class="text" name="sup_fax"
					value="<?=@$row->sup_fax?>" maxlength="150" style="width: 150px" <?=$view?>/>
			</td>
		</tr>
		<tr>
			<td align="right" nowrap>* <?=$AppUI->_('Classification')?></td>
			<td>
				<? echo arraySelect($cls_list,'sup_classification','id="sup_classification" class="text" style="text-transform: capitalize; width: 300px" title="Classification :: Classification is required"',@$row->sup_classification)?>
			</td>
		</tr>
		<!-- Divisions -->
		<tr>
			<td align="right"><?=$AppUI->_('Division')?></td>
			<td>
				<? /*echo arraySelect($division_list,'sup_division',
									'id="idSupDivisionId" size="1" class="text" style="width: 285px" onchange="doChangeDivision()"',
									@$row->sup_division)*/
					echo arraySelect($division_list,'sup_division','size="1" class="text" style="width: 300px"',@$row->sup_division)
				?>
			</td>
		</tr>
		<!-- costcenter -->
		<tr>
			<td align="right" width="80px"><?=$AppUI->_('Cost Center')?></td>
			<td colspan="3" align="left" nowrap="nowrap">
				<input id="idSupCostCenterName" type="text" class="text" name="sup_costcenter_name"
					value="<?=@$row->getCostCenterName()?>" maxlength="100"
					style="width: <?=$view ? 300 : 300?>px" readonly="true"/>
				<img class="ico clickable" src="images/treetable.gif" onclick="doShowCostCenterSelector()"/>
				<input id="idSupCostCenterId" type='hidden' name='sup_costcenter' value='<?=@$row->sup_costcenter?>'>
			</td>
		</tr>
		<tr>
			<td align="right"><?=$AppUI->_('Resource');?></td>
			<td>
				<?=arraySelect( $resourceList, 'sup_resource',
					'id="sup_resource" class="text" style="text-transform: capitalize; width: 300px" title="Resource :: Resource is required"',
					@$row->sup_resource, true )?>
			</td>
		</tr>
		<tr>
			<td align="right">* <?=$AppUI->_('Ledger 1')?></td>
			<td>
				<input type="text" class="text" name="sup_ledger1" id="sup_ledger1"
					value="<?=@$row->sup_ledger1?>" maxlength="255" style="width: 150px" <?=$view?>/>
			</td>
		</tr>
		<tr>
			<td align="right">* <?=$AppUI->_('Ledger 2')?></td>
			<td>
				<input type="text" class="text" name="sup_ledger2" id="sup_ledger2"
					value="<?=@$row->sup_ledger2?>" maxlength="255" style="width: 150px" <?=$view?>/>
			</td>
		</tr>
		</table>
	</td>
</tr>
<tr>
	<td align="center" colspan="2" style="border-top: 1px solid #E0E0E0">
	<? if (!$view) { ?>
		<input type="submit" class="button" value="<?=$AppUI->_('Save')?>"/>
		<input type="button" class="button" value="<?=$AppUI->_('Cancel')?>" onClick="doCloseSup()"/>
	<? } else { ?>
		<input type="button" class="button" value="<?=$AppUI->_('Close')?>" onClick="doCloseSup()"/>
	<? } ?>
	</td>
</tr>
</table>
</form>
<div id="validation_rules" style="visibility: hidden; display: none">
	var fSupCode = new LiveValidation('sup_code');
	fSupCode.add(Validate.Presence );
	fSupCode.add( Validate.Exclusion , {within: <?=$qCL?>});

	var fSupName = new LiveValidation('sup_name');
	fSupName.add(Validate.Presence );
	var fSupLedger1 = new LiveValidation('sup_ledger1');
	fSupLedger1.add(Validate.Presence );
	var fSupLedger2 = new LiveValidation('sup_ledger2');
	fSupLedger2.add(Validate.Presence );
	var fSupStatus = new LiveValidation('sup_status');
	fSupStatus.add(Validate.Presence );
	var fSupType = new LiveValidation('sup_type');
	fSupType.add(Validate.Presence );
	var fSupCls = new LiveValidation('sup_classification');
	fSupCls.add(Validate.Presence );

	new Tips($$('.text'));
</div>